#include "cube.h"
#include "vertex.h"
#include "..\base\SampleBase.h"
#include "..\base\D3DUtil.h"

#define CUBE
#ifdef  CUBE

class TexturedCube : public SampleBase
{
public:
	TexturedCube() : m_box(0), m_texture(0)
	{

	}
	virtual bool Setup()
	{
		m_box = new Cube(m_device);

		D3DLIGHT9 light;
		::ZeroMemory(&light, sizeof(light));
		light.Type = D3DLIGHT_DIRECTIONAL;
		light.Ambient   = D3DXCOLOR(0.8f, 0.8f, 0.8f, 1.0f);
		light.Diffuse   = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
		light.Specular  = D3DXCOLOR(0.2f, 0.2f, 0.2f, 1.0f);
		light.Direction = D3DXVECTOR3(1.0f, -1.0f, 0.0f);

		m_device->SetLight(0, &light);
		m_device->LightEnable(0, true);

		m_device->SetRenderState(D3DRS_NORMALIZENORMALS, true);
		m_device->SetRenderState(D3DRS_SPECULARENABLE, true);

		// Create texture
		D3DXCreateTextureFromFile(m_device, "crate.jpg", &m_texture);

		// Set Texture Filter States
		m_device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
		m_device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
		m_device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);

		// Set the projection matrix
		D3DXMATRIX proj;
		D3DXMatrixPerspectiveFovLH(
			&proj, 
			D3DX_PI * 0.5f,
			(float)width / (float)height,
			1.0f,
			1000.0f);
		m_device->SetTransform(D3DTS_PROJECTION, &proj);

		return true;
	}
	virtual void Cleanup()
	{
		d3dutil::Delete<Cube*>(m_box);
		d3dutil::Release<IDirect3DTexture9*>(m_texture);

	}

	virtual bool Display(float timeDelta)
	{
		if (m_device)
		{
			// Update the scene: update camera position
			static float angle = (3.0f * D3DX_PI) / 2.0f;
			static float high = 0.0f;
			static float angleFy = (1.0f * D3DX_PI) / 2.0f;

			float radius = 4.0f;

			if(::GetAsyncKeyState(VK_LEFT) & 0x8000f)
				angle -= 0.5f * timeDelta;
			if(::GetAsyncKeyState(VK_RIGHT) & 0x8000f)
				angle += 0.5 * timeDelta;
			if(::GetAsyncKeyState(VK_UP) & 0x8000f)
				angleFy += 0.5f * timeDelta;

			if (::GetAsyncKeyState(VK_DOWN) & 0x8000f)
				angleFy -= 0.5f * timeDelta;


			D3DXVECTOR3 position(
				sinf(angleFy) *cosf(angle) * radius, 
				cosf(angleFy) * radius, 
				sinf(angleFy) *sinf(angle) *radius);

			D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
			D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
			D3DXMATRIX V;
			D3DXMatrixLookAtLH(&V, &position, &target, &up);

			m_device->SetTransform(D3DTS_VIEW, &V);

			// Draw the scene
			m_device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 
				0xff000000, 1.0f, 0);
			m_device->BeginScene();

			m_device->SetMaterial(&d3dutil::WHITE_MTRL);
			m_device->SetTexture(0, m_texture);
			m_box->draw(0, 0, 0);

			m_device->EndScene();
			m_device->Present(0, 0, 0, 0);

		}
		return true;
	}

private:
	Cube* m_box;
	IDirect3DTexture9 *m_texture;
};


int WINAPI WinMain(HINSTANCE hinstance,
				   HINSTANCE prevInstance, 
				   PSTR cmdLine,
				   int showCmd)
{
	TexturedCube sample;
	sample.Init(hinstance, width, height, windowed);
	sample.Run();

	return 0;
}

#endif